#pragma once
#include<iostream>
#include<list>
#include<assert.h>
#include<vector>
#include<string>
using namespace std;
namespace YJ
{
	template<class T>
	struct _list_node
	{
		T _val;
		_list_node<T>* _next;
		_list_node<T>* _prev;
		_list_node(const T& val=T())
			:_val(val)
			, _prev(nullptr)
			, _next(nullptr)
		{}
	}; 

	

	template<class T,class Ref,class Ptr>
	struct _list_iterator
	{
		typedef _list_node<T> node;
		typedef _list_iterator<T,Ref,Ptr> self;

		_list_iterator(node* pnode)
			:_pnode(pnode)
		{}
		Ref operator*()
		{
			return _pnode->_val;
		}

		Ptr operator->()
		{
			return &_pnode->_val;
		}

		bool operator!=(const self& s)const
		{
			return _pnode != s._pnode;
		}

		bool operator==(const self& s)const
		{
			return _pnode == s._pnode;
		}

		self& operator++()
		{
			_pnode = _pnode->_next;
			return *this;
		}

		self operator++(int)
		{
			self tmp(*this);
			_pnode = _pnode->_next;
			return tmp;
		}

		self& operator--()
		{
			_pnode = _pnode->_prev;
			return *this;
		}

		self operator--(int)
		{
			self tmp(*this);
			_pnode = _pnode->_prev;
			return tmp;
		}

		node* _pnode;
	};



	template<class T>
	class list
	{
		typedef _list_node<T> node;
	public:
		typedef _list_iterator<T,T&,T*> iterator;
		typedef _list_iterator<T,const T&,const T*> const_iterator;


		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			      //_list_iterator<T>(_head);
			return iterator(_head);
		}

		const_iterator begin()const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end()const
		{
			//_list_iterator<T>(_head);
			return const_iterator(_head);
		}



		list()
		{
			_head = new node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		void push_back(const T& x)
		{
			/*node* newnode = new node(x);
			node* tail = _head->_prev;
			_head->_prev = newnode;
			tail->_next = newnode;
			newnode->_next = _head;
			newnode->_prev = tail;*/
			insert(end(), x);
		}

		void push_front(const T& x)
		{
			insret(begin(), x);
		}

		void pop_back()
		{
			erase(--end());
		}

		void pop_front()
		{
			erase(begin());
		}

		void insert(iterator  pos, const T& x)
		{
			node* cur = pos._pnode;
			node* prev = cur->_prev;
			node* newnode = new node(x);

			prev->_next = newnode;
			newnode->_prev = prev;
			newnode->_next = cur;
			cur->_prev = newnode;
		}

		iterator erase(iterator pos)
		{
			assert(pos._pnode);
			assert(pos != end());
			node* prev = pos._pnode->_prev;
			node* next = pos._pnode->_next;
			delete pos._pnode;
			prev->_next = next;
			next->_prev = prev;
			return iterator(next);
		}
	private:
		node* _head;
	};


	struct Date
	{
		int year=0;
		int month=1;
		int day=1;
	};
	void PrintList(const list<int>& lt)
	{
		list<int>::const_iterator it = lt.begin();
		while (it != lt.end())
		{
			//*it += 1;
			cout << *it << " ";
			++it;
		}
		cout << endl;
	}

	void test_list1()
	{
		list<int> lt;
		lt.push_back(1);
		lt.push_back(2);
		lt.push_back(3);
	
		lt.push_back(4);

		lt.erase(lt.begin());


		list<int>::iterator it = lt.begin();
		while (it != lt.end())
		{
			cout << *it << " ";
			it++;
		}




		/*list<Date> t;
		t.push_back(Date());
		t.push_back(Date());
		t.push_back(Date());
		list<Date>::iterator it = t.begin();
		while (it != t.end())
		{
			cout << it->year << " " << it->month << " " << it->day << " ";
			cout << endl;
			++it;
		}*/

	}
}
